using System;
using Microsoft.CodeAnalysis;

namespace Pinwheel.Toolkits.Generator;

public class ReferenceSymbols
{
    public Compilation Compilation { get; }
    
    public INamedTypeSymbol LSerializeAttribute { get; }
    public INamedTypeSymbol LSerializeOrderAttribute { get; }
    public INamedTypeSymbol LSerializeIgnoreAttribute { get; }
    public INamedTypeSymbol LSerializeIncludeAttribute { get; }
    
    public ReferenceSymbols(Compilation compilation)
    {
        Compilation = compilation;
        LSerializeAttribute = GetTypeByMetadataName(Definition.LSerializeAttribute);
        LSerializeIgnoreAttribute = GetTypeByMetadataName(Definition.LSerializeIgnoreAttribute);
        LSerializeIncludeAttribute = GetTypeByMetadataName(Definition.LSerializeIncludeAttribute);
    }
    
    INamedTypeSymbol GetTypeByMetadataName(string metadataName)
    {
        var symbol = Compilation.GetTypeByMetadataName(metadataName);
        if (symbol == null)
        {
            throw new InvalidOperationException($"Type {metadataName} is not found in compilation.");
        }
        return symbol;
    }
}